<?php
/**
 * 登陆，注册，注销
 *
 *
 */
class Login extends CI_Controller
{
	function __construct()
    {
        parent::__construct();
         date_default_timezone_set("ETC/GMT-8");
    }
   
    
    // --------------------------------------------------------------------

    /**
	 * 登陆，注册 界面
	 *
	 *
	 */	
    function index()
    { 
		
	    $data = array();
		$data['css'][0] = "<link type='text/css' rel='stylesheet' href='/css/login.css'>";
		//$data['css'][1] = "<link type='text/css' rel='stylesheet' href='/css/jq_login.css'>";
	    $data['title'] = '用户登录';

		$data['error_msg'] = $this->uri->segment(3, 0);
	
		$data['preurl'] = $_SERVER['HTTP_REFERER'];
		//$this->session->set_userdata($preURL);
	    $this->load->view('login',$data);
    }
    
    // --------------------------------------------------------------------

    /**
	 * 验证名字是否已存在 ajax
	 *
	 *
	 */		
	function check_name()
	{
		$id = $_POST['id'];
		
		$id_name =  $_POST['id_name'];

 
		$name = $_POST['id_name'];
		$this->load->model('customer_model');
		$msg = array('Result' => true);
		if ($name){
		    if ($this->customer_model->check_name($name)){
				$msg = array('Result' => false);
			}				
		}
        
		echo json_encode($msg);
	}

    // --------------------------------------------------------------------

    /**
	 * 验证邮箱是否已存在 ajax
	 *
	 *
	 */	
	function check_email()
	{
		$id = $_POST['id'];
		$id_name =  $_POST['id_name'];
		$email = $_POST['id_name'];
//        $qs = query_string_to_array();
//	    $email = $qs['tbxRegisterEmail'];
		$this->load->model('customer_model');
		$msg = array('Result' => true);
		if ($email){
		    if ($this->customer_model->check_email($email)){
				$msg = array('Result' => false);
			}				
		}
        
		echo json_encode($msg);
	}
    
    // --------------------------------------------------------------------

    /**
	 * 注册生成验证码
	 *
	 *
	 */	
	function yzm_img()
	{
		$this->load->helper('captcha');
		code();
	}
    
	// --------------------------------------------------------------------

    /**
	 * 登陆生成验证码
	 *
	 *
	 */	
	function yzm_img1()
	{
		$this->load->helper('captcha1');
		code();
	}
    
	// --------------------------------------------------------------------

    /**
	 * 检查注册验证码是否准确
	 *
	 *
	 */	
	function _check_yzm()
	{
		
 
		session_start();
        $Verifier = $this->input->post('tbxRegisterVerifier');
        if ($_SESSION['verify'] == $Verifier){
			//
		}else{
			redirect(base_url().'login/index/verifier');
		}
		return;
	}
    
	// --------------------------------------------------------------------

    /**
	 * 检查登陆验证码是否准确
	 *
	 *
	 */	
	function _check_yzm1()
	{
	 
		session_start();
        $Verifier = $this->input->post('tbxLoginVerifier');
        if (!empty($_SESSION['verify1']) && $_SESSION['verify1'] == $Verifier){
			//
		}else{
			redirect(base_url().'login/index/verifier1');
		}
		return;
	}
    
	// --------------------------------------------------------------------

    /**
	 * 保存注册信息
	 *
	 *
	 */	
	function save()
	{
        //$this->_check_yzm();
        	
		$name = $this->input->post('tbxRegisterNickname');
		$password = $this->input->post('tbxRegisterPassword');
		$email = $this->input->post('tbxRegisterEmail');
        $gourl = $this->input->post('loginPreURL');	
		
        
        $this->load->library('form_validation');
        $this->set_save_form_rules();
        
		if (TRUE == $this->form_validation->run()){
			$this->load->model('customer_model');            
            $this->customer_model->name = $name;
		    $this->customer_model->email = $email;
		    $this->customer_model->password = $password;
		    $this->customer_model->type = "user"; //普通用户, 不是餐馆
			$this->customer_model->create();
            $customer_id = $this->db->insert_id();

			$customer = array(
                   'user_name'  => $name,
				   'user_id'  => $customer_id,
                   'user_in' => TRUE
               );
            $this->session->set_userdata($customer);
            if($gourl)
				redirect($gourl);
			else 
				redirect(base_url().'home');
		}else{
            redirect(base_url().'login/reg_success/error');
		}
	}
    
	// --------------------------------------------------------------------

    /**
	 * 注册验证规则
	 *
	 *
	 */	
	function set_save_form_rules()
    {
        $rules['tbxRegisterNickname'] = 'required|min_length[3]|max_length[50]';	
		$rules['tbxRegisterPassword'] = 'required|min_length[3]|max_length[30]';	
		$rules['tbxRegisterEmail'] = 'required|valid_email';	
		$this->form_validation->set_rules($rules);		
    }
    
	// --------------------------------------------------------------------

    /**
	 * 登出
	 *
	 *
	 */	
	function logout()
	{
		$this->session->sess_destroy();
		redirect(base_url().'home');
	}
    
	// --------------------------------------------------------------------

    /**
	 * 登陆
	 *
	 *
	 */	
	function check_customer()
	{
        $name 		= $this->input->post('tbxLoginNickname');
		$password 	= $this->input->post('tbxLoginPassword');	// 传过来的没有加密,是明文,后续可能要修改为密文
		$go_to_url 		= $this->input->post('loginPreURL');	

		$this->load->model('customer_model');            
        $this->customer_model->name = $name;
		$this->customer_model->password = $password;
		$this->customer_model->ismd5 = 1;	// 是否以md5的加密方式验证密码, 0表示明文验证, 1表示md5方式验证
		
		// 在model里面完成数据库的验证, 查看该用户名和密码是否正确
		$_customer = $this->customer_model->check_customer();

		
		if ($_customer){
			// 登录成功页面
			$customer = array(
				   'user_name' => $_customer['username'],
				   'user_id'   => $_customer['id'],
				   'user_in'  => TRUE,
				   'user_last_login' => $_customer['last_login_at']
			   );
			   
			// 登录状态写入session
			$this->session->set_userdata($customer);
            
			// 把用户的最后登录时间记录到数据库
			$this->customer_model->update_last_login($_customer['id']);
			
			if($go_to_url)
				redirect($go_to_url); // 导向上一个页面
			else 
				redirect(base_url().'home');
		}else{
			// 登录失败页面
            redirect(base_url().'login/index/customer'); 
		}
	}
	
	
	/**
	 * 
	 * 登录成功
	 */
	function login_success(){
		  $this->load->view('login_success');
	}
	function login_fail(){
		$data['error_msg'] = "用户名或密码错误";
		  $this->load->view('login_success', $data);
	}
	function reg_success(){
		  $this->load->view('reg_success');
	}
 
}











